#ifndef _MUSIC_H
#define _MUSIC_H

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int find_music();
void play_music(int);

int find_music()
{
    char *path = "./mp3/";            //find songs in this default dir
    DIR *dir;
    struct dirent *ptr;
    dir = opendir(path);

    int count = 0;
    while ((ptr = readdir(dir)) != NULL)
    {
        count += 1;
    }
    closedir(dir);
    count -= 2;   // under linux os ,every dir has . .. two dir,should skip
    return count;
}

void play_music(int index)
{

    char buffer[80];

    // make string like that :   madplay -o wav:- 1.mp3 | aplay   ,     the song name mue be 1.mp3 2.mp3  ....
    char *path = "madplay -o wav:- ./mp3/";
    sprintf(buffer, "%s%d", path, index);
    strcat(buffer, ".mp3  | aplay &");

    system(buffer);
}

#endif